package com.work.holand.androidstudydemo;

import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.orhanobut.logger.Logger;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName()+"aaa";
    private boolean isTrue;
    @BindView(R.id.button)
    Button mButton;
    private Unbinder mUnbinder;

    @BindView(R.id.button4)
    Button mBtn4ChangeIcon;
    @OnClick(R.id.button4)
    public void changeIcon(View view) {
        ComponentName a = new ComponentName(getBaseContext(), "com.work.holand.androidstudydemo.MainActivity");

        ComponentName c = new ComponentName(getBaseContext(), "com.work.holand.androidstudydemo.ABC");

        PackageManager mPm = getPackageManager();
        mPm.setComponentEnabledSetting(a,
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);
        mPm.setComponentEnabledSetting(c,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.d(TAG, "onNewIntent");
        mUnbinder = ButterKnife.bind(this);  //保存引用
//        initViews();
        boolean b = BuildConfig.DEBUG;
        Intent intent = getIntent();
        int id = intent.getIntExtra(ID, 0);
        isTrue = intent.getBooleanExtra(IS_CHANGE, false);
        mButton.setText(String.valueOf(isTrue));
        Log.d(TAG, "onCreate id->"+id);

    }

    @OnClick(R.id.button)
    public void onBoolean(View v) {
        isTrue = Boolean.parseBoolean(((Button)v).getText().toString());
        isTrue = !isTrue;
        ((Button)v).setText(String.valueOf(isTrue));
        Log.d(TAG, "onBoolean");

        ComponentName a = new ComponentName(getBaseContext(), "com.work.holand.androidstudydemo.MainActivity");

        ComponentName c = new ComponentName(getBaseContext(), "com.work.holand.androidstudydemo.ABC");

        PackageManager mPm = getPackageManager();
        mPm.setComponentEnabledSetting(c,
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);
        mPm.setComponentEnabledSetting(a,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);
    }
    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG, "onRestart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume");

    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
        //解绑 即把上面绑定的view全部置为null
        mUnbinder.unbind();
        Log.d(TAG, mButton==null?"mButton is null":mButton.toString());
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        isTrue = Boolean.parseBoolean((((Button)findViewById(R.id.button)).getText().toString()));
        outState.putBoolean("isTrue", isTrue);
        super.onSaveInstanceState(outState);
        Log.d(TAG, "onSaveInstanceState isTrue->"+isTrue);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        isTrue = savedInstanceState.getBoolean("isTrue");

        Log.d(TAG, "onRestoreInstanceState isTrue->"+isTrue);
        mButton.setText(String.valueOf(isTrue));
    }

    public static final String ID = "id";
    public static final String IS_CHANGE = "is_change";

    /*abc*/
    public void onSkip(View view) {
        Log.d(TAG, "onSkip");
        Logger.d("onSkip");
        Intent intent = new Intent(this, MainActivity.class);

        //注意 这里的 细节 区别 1) 目标Activity存在时,FLAG_ACTIVITY_CLEAR_TOP清理 目标 及以上的Activity 重新实例化 并入栈;
//        2)FLAG_ACTIVITY_SINGLE_TOP 则 不清理自身
//        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        int id = 123;
        intent.putExtra(ID, id);
        intent.putExtra(IS_CHANGE, isTrue);
        startActivity(intent);
        ComponentName a = new ComponentName("com.work.holand.myapplication", "com.work.holand.myapplication.SingleTaskActivity");
//        startApp(a, "SingleTaskActivity");

//        Intent intent = new Intent("android.intent.action.AppOther_OTHER_ACTIVITY");
//        startActivity(intent);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Log.d(TAG, "onConfigurationChanged-->"+(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE?"ORIENTATION_LANDSCAPE":"ORIENTATION_PORTRAIT"));
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        int id = intent.getIntExtra(ID, 0);// 23````````````1  23
        Log.d(TAG, "onNewIntent");
    }

    /*
     * 启动一个app
     */
    public void startAPP(String appPackageName){
        try{
            Intent intent = this.getPackageManager().getLaunchIntentForPackage(appPackageName);
            startActivity(intent);
        }catch(Exception e){
            Toast.makeText(this, "没有安装", Toast.LENGTH_LONG).show();
        }
    }

    /**
     * 启动一个app
     * com -- ComponentName 对象，包含apk的包名和主Activity名
     * param -- 需要传给apk的参数
     */
    private void startApp(ComponentName com, String param) {
        if (com != null) {
            PackageInfo packageInfo;
            try {
                packageInfo = getPackageManager().getPackageInfo(com.getPackageName(), 0);
            } catch (PackageManager.NameNotFoundException e) {
                packageInfo = null;
                Toast.makeText(this, "没有安装", Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }
            try {
                Intent intent = new Intent();
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setComponent(com);
                if (param != null) {
                    Bundle bundle = new Bundle(); // 创建Bundle对象
                    bundle.putString("flag", param); // 装入数据
                    intent.putExtras(bundle); // 把Bundle塞入Intent里面
                }
                startActivity(intent);
            } catch (Exception e) {
                Toast.makeText(this, "启动异常", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
